<h3>User Form</h3>
<div id="notification"></div>
<!-- Horizontal form (default) -->
<form class="horizontal-form" id="frmUser">
    <?php echo $form->renderHiddenFields() ?>
	<fieldset>
		<legend>Details</legend>
		<dl>
			<dt>
				<label class="mandatory"><?php echo $form['first_name']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php echo $form['first_name']->render(array('class'=>'medium'))?>
			</dd>
			<dt>
				<label class="mandatory"><?php echo $form['last_name']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php echo $form['last_name']->render(array('class'=>'medium'))?>
			</dd>
      <dt>
				<label class="mandatory"><?php echo $form['position']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php echo $form['position']->render(array('class'=>'medium'))?>
			</dd>
      <dt>
				<label class="mandatory"><?php echo $form['department']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php echo $form['department']->render(array('class'=>'medium'))?>
			</dd>
			<dt>
				<label class="mandatory"><?php echo $form['email_address']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php if($form->getObject()->isNew()) :?>
			         <?php echo $form['email_address']->render(array('class'=>'medium'))?>
			    <?php else: ?>
			         <?php echo $form['email_address']->render(array('class'=>'medium', 'readOnly'=>'true'))?>
			    <?php endif;?>
			</dd>
      <dt>
				<label class="mandatory"><?php echo $form['mobile_number']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php echo $form['mobile_number']->render(array('class'=>'medium'))?>
			</dd>
			<dt>
				<label class="mandatory"><?php echo $form['password']->renderLabel()?></label>
			</dt>
			<dd>
			    <?php echo $form['password']->render(array('class'=>'short'))?>
			</dd>
			<dt>
                <label class="mandatory"><?php echo $form['validate_password']->renderLabel()?></label>
            </dt>
            <dd>
                <?php echo $form['validate_password']->render(array('class'=>'short'))?>
            </dd>
			<dt>
				<label class="mandatory"><?php echo $form['is_active']->renderLabel()?></label>
			</dt>
			<dd>
			     <?php echo $form['is_active']->render()?>
			</dd>
			<dt>
                <label class="mandatory"><?php echo $form['groups_list']->renderLabel()?></label>
            </dt>
            <dd>
			    <?php echo $form['groups_list'] ?>
			</dd>
		</dl>
	</fieldset>

  <input id="submitUser" style="float: right;" type="button" class="blue small" value="Submit" />
  <input id="cancelUser" type="button" class="blue small" value="Back" />

</form>

<script type="text/javascript">
$(document).ready(function(){

    $("#submitUser").click(function(){
        toggleLoading();
        dataAr = $("#frmUser").serializeArray();
        $.ajax({
            url: 'user/ajaxSave',
            type: 'POST',
            dataType: 'json',
            data: dataAr,
            success: function(response){
               $("#notification").empty();
               if(response.status == 'success') {
                 $('#crud_data').click();
               }
               else {
                 $("#notification").html(response.data);
               }
               if(typeof response.id != "undefined")
               {
               		// set the record id
               		$("#<?php echo $form['id']->renderId()?>").val(response.id);
               		// change the label to Back
               		$("#cancelUser").val("Back");
               		// set edit as selected
               		defSelected = $("#crud_edit");
               		setDefaultSelected();
               		// clear the password field
               		$("#<?php echo $form['password']->renderId()?>").val();
               		$("#<?php echo $form['validate_password']->renderId()?>").val();
               		// make the email readOnly
               		$("#<?php echo $form['email_address']->renderId()?>").attr('readonly', true);

               }
               toggleLoading();
            }

        });
    });

    $("#cancelUser").click(function(){
        $("#tab2-content").empty();
        $('div.formError').remove();
        $("#crud_data").click();
    });

});
</script>